package com.joeyzh.base;

import android.content.Context;
import android.view.View;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Joey on 2018/11/22.
 * 列表的构造工具ViewModel
 *
 * @author Joey
 */

public class ListViewModel<T extends Object> extends IViewModel<List<T>> {


    private HashMap<String, Integer> itemBindMap;
    protected String[] from;
    protected int [] to;

    public ListViewModel(List<T> list) {
        super(list);
        itemBindMap = new HashMap<>();
    }

    public ListViewModel bindListItems(String key, int resId) {
        itemBindMap.put(key, resId);
        return this;
    }

    public ListViewModel builder(View listRoot, Context context, int layoutId) {
        if (itemBindMap.isEmpty()) {
            return this;
        }
        int length = itemBindMap.size();
        from = new String[length];
        to = new int[length];
        itemBindMap.keySet().toArray(from);
        int i = 0;
        for (Integer res : itemBindMap.values()) {
            to[i++] = res.intValue();
        }
        return this;
    }

}
